import Vue from 'vue'
import vueRouter from 'vue-router'
Vue.use(vueRouter)
const asyncCompont = path => import(`@/components/${path}`);
let router=new vueRouter({
    mode: 'hash',
    routes:[
        {
            path: '/wx/home',
            name: 'Home',
            component: () => asyncCompont('home'),
            meta: {
                index: 1,
                sendToken: true,
            }
        },
        {
            path: '/wx/homeSpokesman',//'/wx/homeSpokesman?homeSpokesman=homeSpokesman',
            name: 'homeSpokesman',
            component: () => asyncCompont('homeSpokesman'),
            meta: {
                index: 1,
                sendToken: true,
            }
        },
        {
            path: '/wx/wxSignIn',
            name: 'wxSignIn',
            component: () => asyncCompont('wxSignIn'),
            meta: {
                index: 2,
            }
        },
        {
            path: '/wx/selectCity',
            name: 'selectCity',
            component: () => asyncCompont('selectCity'),
            meta: {
                index: 2,
                sendToken: true,
            }
        },
        {
            path: '/wx/personalDetails',
            name: 'personalDetails',
            component: () => asyncCompont('personalDetails'),
            meta: {
                index: 2,
                sendToken: true,
            }
        },
        {
            path: '/wx/selectPeople',
            name: 'selectPeople',
            component: () => asyncCompont('selectPeople'),
            meta: {
                index: 3,
                sendToken: true,
            }
        },
        {
            path: '/wx/shareImg',
            name: 'shareImg',
            component: () => asyncCompont('shareImg'),
            meta: {
                index: 1,
                sendToken: true,
            }
        },
        {
            path: '/wx/activeRule',
            name: 'activeRule',
            component: () => asyncCompont('activeRule'),
            meta: {
                index: 2,
            }
        },
        {
            path: '/wx/uploadimg',
            name: 'uploadimg',
            component: () => asyncCompont('uploadimg'),
            meta: {
                index: 2,
            }
        },
        {
            path: '/wx/invitationRecord',
            name: 'invitationRecord',
            component: () => asyncCompont('invitationRecord'),
            meta: {
                index: 3,
            }
        },
        {
            path: '/wx/editImg',
            name: 'editImg',
            component: () => asyncCompont('editImg'),
            meta: {
                index: 3,
            }
        },
        {
            path: '/wx/editText',
            name: 'editText',
            component: () => asyncCompont('editText'),
            meta: {
                index: 4,
            }
        },
        {
            path: '*',
            name: 'Home',
            component: () => asyncCompont('home'),
            meta: {
                index: 1,
                sendToken: true,
            }
        }
    ]
})
//全局路由权限校验-token
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.sendToken)) {
        let token = localStorage.getItem("token")
        if (token && token !== '') {
            var instance = Vue.prototype.axios.create({});
            // instance.defaults.headers.common["token"] = 'oyr73Bw5cTBreA';
            next();
        }
        else {
            if (!from.matched[0] || !from.matched[0].instances) {
                next();
            } else {
                const instances = from.matched[0].instances.default;
                //这里需要自动登录操作
                // instances.$p_message('请先登录', 'error');
                // setTimeout(() => {
                //     if(instances.$route.query.homeSpokesman){
                //         next({
                //             path: '/wx/homeSpokesman'
                //         })
                //     }else{
                //         next({
                //             path: '/wx/home'
                //         })
                //     }
                    
                // }, 800)
            }
        }
        if (to.meta.title) {
            document.title = to.meta.title
        } else {
            document.title = '畅享图书汇'
        }
    } else {
        if (to.meta.title) {
            document.title = to.meta.title
        }else{
            document.title = '畅享图书汇'
        }
        next();
    }
    
});
export default router
